package com.buddy.design.pattern._05strategy;


import com.buddy.design.pattern._05strategy.pay.MsgResult;
import com.buddy.design.pattern._05strategy.pay.Order;
import com.buddy.design.pattern._05strategy.pay.PayStrategy;
import com.buddy.design.pattern._05strategy.promotion.PromotionActivity;
import com.buddy.design.pattern._05strategy.promotion.PromotionFactory;
import org.junit.Test;

public class StrategyPattern {

    /*
     *  策略模式：
     *      定义了算法家族，分别封装起来，可以让他们互相替换，此模式可以让算法的变换不会影响到使用算法的用户
     *  可以避免多重if else 等以及switch等
     *
     *  应用场景：
     *      假如系统中有很多类，但他们的区别仅仅在于行为不同；
     *      一个系统需要动态的在几种算法中选择一种
     *   JDK->Comparator
     *   Spring->Resource InstantiationStrategy
     *
     *
     *   策略模式符合开闭原则；
     *   避免用多重条件转移语句，if else/switch
     *   提高算法的保密性和安全性；
     *
     *   客户端必须知道全部策略，并自行决定使用哪种策略；
     *   代码中会产生非常多的策略类，不利于代码维护
     *
     */

    @Test
    public void promotionTest() {
//        PromotionActivity activity_618 = new PromotionActivity(new CouponPromotion());
//        PromotionActivity activity_1111 = new PromotionActivity(new CashBackPromotion());
//        activity_618.execute();
//        activity_1111.execute();

//        PromotionActivity activity = null;
//        String promotionKey = "COUPON" ;
//        if (StringUtils.equals(promotionKey,"COUPON")){
//            activity = new PromotionActivity(new CouponPromotion());
//        }else if(StringUtils.equals(promotionKey,"CASH")){
//            activity = new PromotionActivity(new CashBackPromotion());
//        }else if (StringUtils.equals(promotionKey,"GROUP")){
//            activity = new PromotionActivity(new GroupBuyPromotion());
//        }else{
//            activity = new PromotionActivity(new EmptyPromotion());
//        }
//        activity.execute();

        String promotionKey = "222";
        PromotionActivity activity = new PromotionActivity(PromotionFactory.getPromotionStrategy(promotionKey));
        activity.execute();
    }

    @Test
    public void payTest() {
        Order order = new Order("1", "20200901", 80);
//        MsgResult msgResult = order.pay(PayStrategy.ALI_PAY);
        MsgResult msgResult = order.pay(PayStrategy.UNION_PAY);
        System.out.println(msgResult.toString());

//        Comparator Arrays TreeMap
//        Resource
    }

}
